using System;
using System.IO;
using System.Collections.Generic;

namespace BanZuiCloudControlAgent.Utils.Common
{
    /// <summary>
    /// 配置管理器，统一配置访问入口
    /// </summary>
    public static class ConfigurationManager
    {
        private static readonly NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
        private static readonly string _configRootDirectory = @"C:\脚本配置文件\云控系统配置";
        private static readonly string _databaseDirectory;
        private static readonly string _logDirectory;
        private static readonly string _tempDirectory;
        private static readonly string _configFilesDirectory;
        
        /// <summary>
        /// 静态构造函数，初始化目录
        /// </summary>
        static ConfigurationManager()
        {
            try
            {
                _databaseDirectory = Path.Combine(_configRootDirectory, "Database");
                _logDirectory = Path.Combine(_configRootDirectory, "Logs");
                _tempDirectory = Path.Combine(_configRootDirectory, "Temp");
                _configFilesDirectory = Path.Combine(_configRootDirectory, "Config");
                
                // 创建所有必要的目录
                InitializeDirectories();
                
                _logger.Info("配置管理器初始化完成");
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "配置管理器初始化失败");
            }
        }
        
        /// <summary>
        /// 初始化配置目录结构
        /// </summary>
        public static void InitializeDirectories()
        {
            try
            {
                // 创建所有必要的目录
                CreateDirectoryIfNotExists(_configRootDirectory);
                CreateDirectoryIfNotExists(_databaseDirectory);
                CreateDirectoryIfNotExists(_logDirectory);
                CreateDirectoryIfNotExists(_tempDirectory);
                CreateDirectoryIfNotExists(_configFilesDirectory);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "初始化配置目录结构失败");
                throw;
            }
        }
        
        /// <summary>
        /// 如果目录不存在则创建
        /// </summary>
        private static void CreateDirectoryIfNotExists(string path)
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
        }
        
        /// <summary>
        /// 获取数据库文件的完整路径
        /// </summary>
        public static string GetDatabaseFilePath(string fileName)
        {
            try
            {
                // 确保目录存在
                if (!Directory.Exists(_databaseDirectory))
                {
                    Directory.CreateDirectory(_databaseDirectory);
                }
                return Path.Combine(_databaseDirectory, fileName);
            }
            catch (Exception ex)
            {
               _logger.Error(ex, "获取数据库文件路径失败");
                // 如果出现异常，回退到应用程序目录下的Data文件夹
                string appDir = AppDomain.CurrentDomain.BaseDirectory;
                string dataDir = Path.Combine(appDir, "Data");

                // 确保Data目录存在
                if (!Directory.Exists(dataDir))
                {
                    Directory.CreateDirectory(dataDir);
                }

                return Path.Combine(dataDir, fileName);
            }
        }
        
        /// <summary>
        /// 获取日志文件的路径
        /// </summary>
        public static string GetLogFilePath(string fileName)
        {
            return Path.Combine(_logDirectory, fileName);
        }
        
        /// <summary>
        /// 获取临时文件的路径
        /// </summary>
        public static string GetTempFilePath(string fileName)
        {
            return Path.Combine(_tempDirectory, fileName);
        }
        
        /// <summary>
        /// 获取配置文件的完整路径
        /// </summary>
        public static string GetConfigFilePath(string fileName)
        {
            return Path.Combine(_configFilesDirectory, fileName);
        }
        
        #region 配置访问方法
        
        /// <summary>
        /// 获取配置服务实例
        /// </summary>
        public static ConfigurationService ConfigService => ConfigurationService.Instance;
        
        /// <summary>
        /// 获取应用程序配置
        /// </summary>
        public static AppSettings AppSettings => new AppSettings { UI = UI };
        
        /// <summary>
        /// 获取UI配置
        /// </summary>
        public static UISettings UI => ConfigService.GetSection<UISettings>("UI");
        
        /// <summary>
        /// 获取字符串类型的配置值
        /// </summary>
        /// <param name="key">配置键，支持"Section:Key"格式</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        public static string GetString(string key, string defaultValue = "")
        {
            return ConfigService.GetValue(key, defaultValue);
        }
        
        /// <summary>
        /// 获取整数类型的配置值
        /// </summary>
        /// <param name="key">配置键，支持"Section:Key"格式</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        public static int GetInt(string key, int defaultValue = 0)
        {
            return ConfigService.GetValue<int>(key, defaultValue);
        }
        
        /// <summary>
        /// 获取布尔类型的配置值
        /// </summary>
        /// <param name="key">配置键，支持"Section:Key"格式</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        public static bool GetBool(string key, bool defaultValue = false)
        {
            return ConfigService.GetValue<bool>(key, defaultValue);
        }
        
        /// <summary>
        /// 获取嵌套的配置值
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="section">配置节</param>
        /// <param name="key">配置键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        public static T GetNestedValue<T>(string section, string key, T defaultValue = default)
        {
            return ConfigService.GetNestedValue<T>(section, key, defaultValue);
        }
        
        /// <summary>
        /// 设置配置值
        /// </summary>
        /// <param name="key">配置键，支持"Section:Key"格式</param>
        /// <param name="value">配置值</param>
        public static void SetValue(string key, object value)
        {
            ConfigService.SetValue(key, value);
        }
        
        /// <summary>
        /// 设置嵌套的配置值
        /// </summary>
        /// <param name="section">配置节</param>
        /// <param name="key">配置键</param>
        /// <param name="value">配置值</param>
        public static void SetNestedValue(string section, string key, object value)
        {
            ConfigService.SetNestedValue(section, key, value);
        }
        
        /// <summary>
        /// 保存配置
        /// </summary>
        public static void SaveConfig()
        {
            ConfigService.SaveConfig();
        }
        
        /// <summary>
        /// 重新加载配置
        /// </summary>
        public static void ReloadConfig()
        {
            ConfigService.ReloadConfig();
        }
        

        #endregion
    }
} 